package z_guestbook;

import java.awt.EventQueue;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.Vector;

import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class Select extends JInternalFrame {
	GuestBookMain main;
	
	private JLabel lblNewLabel;
	private JTextField findStr;
	private JButton btnNewButton;
	private JScrollPane scrollPane;
	private JTable table;


	public Select(GuestBookMain main) {
		super("방명록 조회", true, true, true, true);
		setDefaultCloseOperation(JInternalFrame.HIDE_ON_CLOSE);
		this.main = main;
		
		setBounds(100, 100, 564, 456);
		getContentPane().setLayout(null);
		getContentPane().add(getLblNewLabel());
		getContentPane().add(getFindStr());
		getContentPane().add(getBtnNewButton());
		getContentPane().add(getScrollPane());

		setData();
	}

	public void setData(){
		String sql = "select serial, mid, subject, mdate from guestbook where " +
	         " mid=? or subject like ? or content like ? ";
		String find = findStr.getText();
		
		try{
			PreparedStatement pstmt = 
				main.conn.prepareStatement(sql);
			pstmt.setString(1, find);
			pstmt.setString(2, "%" + find + "%");
			pstmt.setString(3, "%" + find + "%");
			
			ResultSet rs = pstmt.executeQuery();
			
			//컬럼명 테이블에 셋팅
			ResultSetMetaData rsm = rs.getMetaData();
			Vector col = new Vector<>();
			for(int i=1 ; i<=rsm.getColumnCount() ; i++){
				col.add(rsm.getColumnName(i));
			}
			DefaultTableModel model =
				new DefaultTableModel(col,0);
			table.setModel(model);
			
			//데이터 추가
			while(rs.next()){
				Vector d = new Vector<>();
				for(int i=1 ; i<=rsm.getColumnCount() ; i++){
					d.add(rs.getString(i));
				}
				model.addRow(d);
			}
			
			
			
			
			
			
			
		}catch(Exception ex){
			
		}
		
	}
	
	
	private JLabel getLblNewLabel() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel("찾는 자료");
			lblNewLabel.setBounds(12, 10, 57, 15);
		}
		return lblNewLabel;
	}
	private JTextField getFindStr() {
		if (findStr == null) {
			findStr = new JTextField();
			findStr.addKeyListener(new KeyAdapter() {
				@Override
				public void keyReleased(KeyEvent ev) {
					if(ev.getKeyCode() == KeyEvent.VK_ENTER){
						setData();
					}
				}
			});
			findStr.setBounds(81, 7, 346, 21);
			findStr.setColumns(10);
		}
		return findStr;
	}
	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("검색");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					setData();
				}
			});
			btnNewButton.setBounds(439, 6, 97, 23);
		}
		return btnNewButton;
	}
	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			
			scrollPane.setBounds(12, 49, 524, 368);
			scrollPane.setViewportView(getTable());
		}
		return scrollPane;
	}
	private JTable getTable() {
		if (table == null) {
			table = new JTable();
			table.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseReleased(MouseEvent e) {
					if(main.update == null){
						main.openUpdate();
					}
					main.update.setVisible(true);
					main.update.toFront();
					
					int row = table.getSelectedRow();
					int serial = Integer.parseInt((String)table.getValueAt(row, 0));

					main.update.select(serial);
				}
			});
		}
		return table;
	}
}
